package servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import dao.*;
import entity.*;

/**
 * Servlet implementation class Index_Date
 */
@WebServlet("/Index_Date")
public class Index_Date extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Index_Date() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}
	
	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		List<Theme> themes = ThemeDAO.getInstance().list();
		request.setAttribute("themes", themes);
		
		//加载最新评论
		List<Comment> comments = CommentDAO.getInstance().list_new(0, 5);
		request.setAttribute("comments", comments);
		
		String type = request.getParameter("type");
		if(type==null||type.equals("1")) {//如果type为null或者1，则按时间顺序显示article
			List<Article> articles = ArticleDAO.getInstance().list_top();
			List<Article> articles2 = ArticleDAO.getInstance().list_untop();
			for (Article a : articles2) {
				articles.add(a);
			}
			request.setAttribute("articles", articles);
		}else if(type.equals("2")) {//如果type为2，则按搜索显示
			
		}else if(type.equals("3")){//如果type为2，则按点击的主题显示
			int theme_id = Integer.parseInt(request.getParameter("theme_id"));
			List<Article> articles = ArticleDAO.getInstance().list_top(theme_id);
			List<Article> articles2 = ArticleDAO.getInstance().list(theme_id);
			for (Article a : articles2) {
				articles.add(a);
			}
			request.setAttribute("articles", articles);
		}else {//最后的这波判断用来做健壮性
			List<Article> articles = ArticleDAO.getInstance().list_top();
			List<Article> articles2 = ArticleDAO.getInstance().list_untop();
			for (Article a : articles2) {
				articles.add(a);
			}
			request.setAttribute("articles", articles);
		}
		HttpSession session =request.getSession(true);
		
		List<Announcement> ans = AnnouncementDAO.getAll();
		Announcement an = new Announcement(0,"default");
		for (int i = 0; i < ans.size(); i++) {
			 if(an.getA_id()<=ans.get(i).getA_id()){		 
				 an = ans.get(i);
			 }
			
        }
		session.setAttribute("an", an.getContent());
		request.getRequestDispatcher("index.jsp").forward(request, response);
		//别忘了还有分页的判断
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
